import pymysql
import requests
from fake_useragent import UserAgent
from lxml import etree
import pymysql

# 解析



def get_url(url):
    headers = {
        'User-Agent': UserAgent().random,
        'Cookie': '__mta = 20387463.1602038390164.1604495609374.1604496472797.59;__mta = 20387463.1602038390164.1604496472797.1604496708845.60;_lxsdk_cuid = 17500eddc39c8 - 002d9cf42cd111 - 333376b - 144000 - 17500eddc3ac8;uuid_n_v = v1;_lx_utm = utm_source % 3DBaidu % 26utm_medium % 3Dorganic;__mta = 20387463.1602038390164.1604496686047.1604496873540.62;uuid = CD7871901EA211EB84B26FC517E6DD3D85C1021C288E4511AFED5210341DB0FA;_csrf = 373f0308de1358f9e8c117decdf84be764f44e3d1441ca384c7f19da73b82a66;lt = ImjM6PgSgoQBB_d1vENeXwEcg6EAAAAA6AsAAJl7oF0dzALugToEJ1irJlmDDKacXO2CAoxBdeh6REmXG4mGPbkvejSF0e8SjxSHXQ;lt.sig = YmjSS8rQAaLTB0DL8_PGHp_XprU;_lxsdk = CD7871901EA211EB84B26FC517E6DD3D85C1021C288E4511AFED5210341DB0FA;Hm_lvt_703e94591e87be68cc8da0da7cbd0be2 = 1604494740, 1604496686, 1604496873, 1604497014;Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2 = 1604497014;__mta = 20387463.1602038390164.1604496472797.1604497014387.60;_lxsdk_s = 1759356caa8 - 922 - dad - 1b5 % 7C % 7C116'
    }
    resp = requests.get(url, headers=headers)
    resp.encoding = 'utf-8'
    # print(resp.text)
    return resp.text


# 解析
def parse_info(html):
    db = pymysql.connect("192.168.41.128", "root", "123456", "maoyan_demo")
    cursor = db.cursor()
    e = etree.HTML(html)
    name = e.xpath('//p[@class="name"]/a/text()')
    Starring = e.xpath('//div[@class="movie-item-info"]/p[2]//text()')
    a = e.xpath('//dd/a/@href')
    img = e.xpath("//img/@data-src")
    list = ['https://trade.maoyan.com{}'.format(i) for i in a]

    for q,w,e,r in zip(name,Starring,img,list):
        p = (q,w,e,r)
        sql = "INSERT INTO movies(name,Starring,imgs,a) VALUES (%s, %s,%s,%s)"
        cursor.execute(sql, p)
        db.commit()
def main():
    base_url = 'https://maoyan.com/board/4?offset={}'
    for url in range(0, 10):
        html = get_url(base_url.format(url * 10))
        parse_info(html)



if __name__ == '__main__':
    main()
